package com.school.controller.admin;

import com.school.common.ServiceResultEnum;
import com.school.controller.vo.OrderItemVO;
import com.school.entity.Order;
import com.school.service.OrderService;
import com.school.util.PageQueryUtil;
import com.school.util.PageResult;
import com.school.util.Result;
import com.school.util.ResultGenerator;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * ClassName: OrderController
 * Package: com.school.controller.admin
 * Description:
 *
 * @Author laohe
 * @Create 2025-07-15 9:06
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/orders")
    public String orders(HttpServletRequest request) {
        request.setAttribute("path", "tea_shop_order");
        return "admin/tea_shop_order";
    }

    @GetMapping("/orders/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> params) {
        if (StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        // 分页操作
        PageQueryUtil pageQueryUtil = new PageQueryUtil(params);
        // 创建业务层对象 根据业务层提供的业务方法做查询操作
        PageResult pageResult = orderService.getOrdersPage(pageQueryUtil);
        return ResultGenerator.genSuccessResult(pageResult);
    }


    @PostMapping("/orders/update")
    @ResponseBody
    public Result update(@RequestBody Order order) {
        // 参数校验
        if (Objects.isNull(order.getOrderId()) ||
                StringUtils.isEmpty(order.getUserAddress()) ||
                Objects.isNull(order.getTotalPrice())) {
            return ResultGenerator.genFailResult("参数异常！");
        }

        // 调用服务层方法
        String result = orderService.updateOrderInfo(order);

        // 结果处理
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    @PostMapping("/orders/checkDone")
    @ResponseBody
    public Result checkDone(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = orderService.checkDone(ids);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    @PostMapping("/orders/checkOut")
    @ResponseBody
    public Result checkOut(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = orderService.checkOut(ids);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }
    }

    @PostMapping("/orders/close")
    @ResponseBody
    public Result close(@RequestBody Long[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult("参数异常！");
        }
        String result = orderService.closeOrder(ids);
        if (ServiceResultEnum.SUCCESS.getResult().equals(result)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(result);
        }

    }


    @GetMapping("/order-items/{orderId}")
    @ResponseBody
    public Result info(@PathVariable("orderId") Long orderId) {
        List<OrderItemVO> orderItems = orderService.getOrderItemsByOrderId(orderId);
        if (orderItems.size() != 0) {
            return ResultGenerator.genSuccessResult(orderItems);
        } else {
            return ResultGenerator.genFailResult(ServiceResultEnum.DB_ERROR.getResult());
        }
    }

}